﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Web;
using Resolve;
using Resolve.Core;
using Resolve.FlagInfo;

namespace Tester.Tester
{
    /// <summary>
    /// Tester 的摘要说明
    /// </summary>
    public class TestString : IHttpHandler
    {
        private HttpResponse _response;
        private HttpRequest _request;
        private HttpServerUtility _server;

        public void ProcessRequest(HttpContext context)
        {
            _response = context.Response;
            _request = context.Request;
            _server = context.Server;
            string test = _request.QueryString["test"];
            if (string.IsNullOrWhiteSpace(test))
            {
                return;
            }
            test = test.ToLower();
            switch (test)
            {
                case "1":
                    TestSearchFlag();
                    break;
                case "2":
                    TestCombineCode();
                    break;
                case "3":
                    TestCompileCode();
                    break;
                case "4":
                    TestResolve();
                    break;
                case "5":
                    TestFlag();
                    break;
            }
        }

        private FlagCodeInFile GetValue()
        {
            StreamReader sr = new StreamReader(_server.MapPath("~/FileTemplate/Test.txt"));
            string content = sr.ReadToEnd();
            sr.Close();
            FlagFinderFromString searchFlag = new FlagFinderFromString { Source = content };
            FlagCodeInFile flagInCode = searchFlag.FindFlags();
            flagInCode.ClassName = "FileTemplate_Test";
            if (flagInCode.LsCode == null || flagInCode.LsCode.Count == 0)
            {
                return flagInCode;
            }
            return flagInCode;
        }

        private void TestSearchFlag()
        {
            FlagCodeInFile flagInCode = GetValue();
            if (flagInCode == null)
            {
                return;
            }
            foreach (FlagCode fc in flagInCode.LsCode)
            {
                _response.Write(fc.FlagStyle + ":\r\n" + fc.Code + "\r\n\r\n\r\n");
            }
        }

        private void TestCombineCode()
        {
            FlagCodeInFile flagInCode = GetValue();
            if (flagInCode == null)
            {
                return;
            }
            CodeGenerater compileFlag = new CodeGenerater { FlagCodeInFile = flagInCode };
            _response.Write(compileFlag.CombineCode());
        }

        private void TestCompileCode()
        {
            //FlagCodeInFile flagInCode = GetValue();
            //if (flagInCode == null)
            //{
            //    return;
            //}
            //string className = "FileTemplate_Test";

            //CodeGenerater compileFlag = new CodeGenerater { FlagCodeInFile = flagInCode };
            //CodeCompiler compileCode = new CodeCompiler();
            //CodeInfo codeInfo = compileFlag.CombineCode();
            //string strUsing = codeInfo.LsUsing.Aggregate(string.Empty, (current, s) => current + s);
            //string code = strUsing + codeInfo.BodyCode;
            //object obj = CodeCompiler.Compile(code).CreateInstance(ResolveConfig.CompileNameSpace + "." + className);
            //if (obj == null) return;
            //Type type = obj.GetType();
            //MethodInfo[] methodInfos = type.GetMethods();
            //foreach (MethodInfo m in methodInfos)
            //{
            //    _response.Write(m.Name + "<br />");
            //}
        }

        private void TestResolve()
        {
            StreamReader sr = new StreamReader(_server.MapPath("~/FileTemplate/Test.txt"));
            string content = sr.ReadToEnd();
            sr.Close();
            ResolveEngineForFile resolveEngine = new ResolveEngineForFile();
            //ResolveEngineForFile resolveEngine = new ResolveEngineForFile(new FlagFinderFromString(), null, null, new HtmlGeneraterFromString());
            //ResolveEngine.CodeCompiler = null;
            //ResolveEngine.CodeGeneraterBase = null;
            //ResolveEngine.HtmlGenerater = new HtmlGeneraterFromString();
            //ResolveEngine.FlagFinder = new FlagFinderFromString();
            IDictionary<string, object> viewData = new Dictionary<string, object>();
            viewData.Add("Test", "this is a test");
            resolveEngine.ViewData = viewData;
            //_response.Write(resolveEngine.Resolve(1, content, "FileTemplate_Test"));
        }

        private void TestFlag()
        {
            FlagCodeInFile flagInCode = GetValue();
            string str = string.Empty;
            foreach (FlagCode fc in flagInCode.LsCode)
            {
                str += fc.FlagStyle.ToString() + "   start=" + fc.Start + " end=" + fc.End + " " + fc.Code + "<br />";
            }
            _response.Write(str);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}